
import request from '@/app/utils/request'

// 登陆
export const Login = (data: any) => {
    return request({
        url: '/console/api/login',
        method: 'post',
        data
    })
}

// 知识库列表
// curl --location --request GET 'http://183.201.231.29/v1/datasets?page=1&limit=20' \
// --header 'Authorization: Bearer {api_key}'
export const GetDatasets = (key: string) => {
    return request.get(`v1/datasets?page=1&limit=20`, { token: key })
}



// 知识库文档列表
// curl --location --request GET 'http://183.201.231.29/v1/datasets/{dataset_id}/documents' \
// --header 'Authorization: Bearer {api_key}'
export const GetDocuments = (dataset_id: string, key: string) => {
    return request.get(`v1/datasets/${dataset_id}/documents`, { token: key })
}


// 创建空知识库
// curl --location --request POST 'http://183.201.231.29/v1/datasets' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json' \
// --data-raw '{"name": "name", "permission": "only_me"}'
/**
 * 
 * @param {string} data.name 知识库名称（必填） 
 * @param {string} data.description 知识库描述（选填）
 * @param {string} data.indexing_technique 索引模式（选填，建议填写） high_quality(高质量) | economy(经济) 
 * @param {string} data.permission 权限（选填，默认 only_me） only_me(仅自己) | all_team_members(所有团队成员) | partial_members(部分团队成员) 
 * @param {string} data.Provider （选填，默认 vendor）   vendor(上传文件) | external(外部知识库)
 * @param {string} data.external_knowledge_api_id 外部知识库 API_ID（选填）
 * @param {string} data.external_knowledge_id 外部知识库 ID（选填） 
 * @param {string} key 请求key
 * @returns 
 */
export const CreateDatasets = (data: any, key: string) => {
    return request.post(`v1/datasets`, data, { token: key })
}

// 删除知识库
// curl --location --request DELETE 'http://183.201.231.29/v1/datasets/{dataset_id}' \
// --header 'Authorization: Bearer {api_key}'
export const DeleteDatasets = (dataset_id: string, key: string) => {
    return request.delete(`v1/datasets/${dataset_id}`, { token: key })
}

// 查询文档分段
// curl --location --request GET 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}/segments' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json'
export const GetSegments = (dataset_id: string, document_id: string, key: string) => {
    return request.get(`v1/datasets/${dataset_id}/documents/${document_id}/segments`, { token: key })
}

// 添加分段
// curl --location --request POST 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}/segments' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json' \
// --data-raw '{"segments": [{"content": "1","answer": "1","keywords": ["a"]}]}'
export const AddSegments = (dataset_id: string, document_id: string, data: any, key: string) => {
    return request.post(`v1/datasets/${dataset_id}/documents/${document_id}/segments`, data, { token: key })
}


// 删除文档分段
// curl --location --request DELETE 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json'
export const DeleteSegments = (dataset_id: string, document_id: string, segment_id: string, key: string) => {
    return request.delete(`v1/datasets/${dataset_id}/documents/${document_id}/segments/${segment_id}`, { token: key })
}

// 更新分段
// curl --location --request POST 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json'\
// --data-raw '{"segment": {"content": "1","answer": "1", "keywords": ["a"], "enabled": false}}'
export const UpdataSegments = (dataset_id: string, document_id: string, segment_id: string, data: any, key: string) => {
    return request.post(`v1/datasets/${dataset_id}/documents/${document_id}/segments/${segment_id}`, data, { token: key })
}

// 查询文档分段
// curl --location --request GET 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}/segments' \
// --header 'Authorization: Bearer {api_key}' \
// --header 'Content-Type: application/json'
export const QuerySegments = (dataset_id: string, document_id: string, keyword: any, key: string) => {
    return request.get(`v1/datasets/${dataset_id}/documents/${document_id}/segments?keyword=${keyword}`, { token: key })
}


// 文本检索
// curl --location --request POST 'http://183.201.231.29/v1/datasets/{dataset_id}/retrieve' \
// --header 'Authorization: Bearer {api_key}'\
// --header 'Content-Type: application/json'\
// --data-raw '{
//   "query": "test",
//   "retrieval_model": {
//       "search_method": "keyword_search",
//       "reranking_enable": false,
//       "reranking_mode": null,
//       "reranking_model": {
//           "reranking_provider_name": "",
//           "reranking_model_name": ""
//       },
//       "weights": null,
//       "top_k": 1,
//       "score_threshold_enabled": false,
//       "score_threshold": null
//   }
// }'

export const Retrieve = (dataset_id: string, data: any, key: string) => {
    return request.post(`v1/datasets/${dataset_id}/retrieve`, data, {
        token: key,
        headers: { 'Content-Type': 'application/json' }
    })
}

// 往知识库添加文件
// curl --location --request POST 'http://183.201.231.29/v1/datasets/{dataset_id}/document/create-by-file' \
// --header 'Authorization: Bearer {api_key}' \
// --form 'data="{"indexing_technique":"high_quality","process_rule":{"rules":{"pre_processing_rules":[{"id":"remove_extra_spaces","enabled":true},{"id":"remove_urls_emails","enabled":true}],"segmentation":{"separator":"###","max_tokens":500}},"mode":"custom"}}";type=text/plain' \
// --form 'file=@"/path/to/file"'
export const AddDatasets = (dataset_id: string, formData: any, key: string) => {
    return request.post(`v1/datasets/${dataset_id}/document/create-by-file`, formData, { token: key })
}

// 删除知识库文件
// curl --location --request DELETE 'http://183.201.231.29/v1/datasets/{dataset_id}/documents/{document_id}' \
// --header 'Authorization: Bearer {api_key}'
export const DeleteWord = (dataset_id: string, document_id: string, key: string) => {
    return request.delete(`v1/datasets/${dataset_id}/documents/${document_id}`, { token: key })
}



 